package com.kingyun.gpsinspection.purificationservice.services.jsm.outerservice.impl;

import com.kingyun.gpsinspection.purificationservice.facade.model.jsm.Ztree;
import com.kingyun.gpsinspection.purificationservice.facade.service.jsm.OutZtreeService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by sugongche on 2017/5/12.
 */
public class OutZtreeServiceImpl implements OutZtreeService {
    /**
     * 从树形集合中获取当前节点的祖先节点：不包括当前节点
     * @param currentNode
     * @param ztreeList
     * @param resultZtreeList
     * @return
     */
    @Override
    public void getAncestorNodeList(Ztree currentNode, List<Ztree> ztreeList, List<Ztree> resultZtreeList) {
        for(int i=0; i<ztreeList.size(); i++){
            if(currentNode != null && currentNode.getpId() != null ){
                if(currentNode.getpId().equals(ztreeList.get(i).getId())){
                    resultZtreeList.add(ztreeList.get(i));

                    getAncestorNodeList(ztreeList.get(i), ztreeList, resultZtreeList);
                }
            }
        }
    }

    /**
     * 从树形集合中获得当前节点的后代节点：不包括当前节点
     * @param currentNode
     * @param ztreeList
     * @param resultZtreeList
     * @return
     */
    @Override
    public void getDescendantNodeList(Ztree currentNode, List<Ztree> ztreeList, List<Ztree> resultZtreeList) {
        for(int i=0; i<ztreeList.size(); i++){
            if(currentNode != null && currentNode.getpId() != null ){
                if(currentNode.getId().equals(ztreeList.get(i).getpId())){
                    resultZtreeList.add(ztreeList.get(i));

                    getDescendantNodeList(ztreeList.get(i), ztreeList, resultZtreeList);
                }
            }
        }
    }

    /**
     * 去除重复节点
     * @param list
     */
    public void removeDuplicate(List<Ztree> list) {
        for (int i = 0; i < list.size() - 1; i++) {
            for (int j = list.size() - 1; j > i; j--) {
                if(list.get(j).getpId() != null && list.get(j).getId()!=null){
                    if (list.get(j).getpId().equals(list.get(i).getpId()) && list.get(j).getId().equals(list.get(i).getId())) {
                        list.remove(j);
                    }
                }

            }
        }
        System.out.println(list);
    }

    public static void main(String[] args) {
        List<Ztree> totalZtreeList = new ArrayList<>();
        List<Ztree> resultZtreeList = new ArrayList<>();
        Ztree ztree = new Ztree();
        ztree.setId("1");
        ztree.setpId("2");
        totalZtreeList.add(ztree);
        ztree = new Ztree();
        ztree.setId("2");
        ztree.setpId("3");
        totalZtreeList.add(ztree);
        ztree = new Ztree();
        ztree.setId("3");
        ztree.setpId("4");
        totalZtreeList.add(ztree);
        ztree = new Ztree();
        ztree.setId("4");
        ztree.setpId("5");
        totalZtreeList.add(ztree);
        ztree = new Ztree();
        ztree.setId("5");
        ztree.setpId("6");
        totalZtreeList.add(ztree);
        ztree = new Ztree();
        ztree.setId("6");
        ztree.setpId("7");
        totalZtreeList.add(ztree);
        ztree = new Ztree();
        ztree.setId("7");
        ztree.setpId("8");
        totalZtreeList.add(ztree);
        ztree = new Ztree();
        ztree.setId("8");
        ztree.setpId("9");
        totalZtreeList.add(ztree);
        ztree = new Ztree();
        ztree.setId("9");
        ztree.setpId("10");
        totalZtreeList.add(ztree);
        ztree = new Ztree();
        ztree.setId("10");
        ztree.setpId("11");
        totalZtreeList.add(ztree);
        ztree = new Ztree();
        ztree.setId("11");
        ztree.setpId("12");
        totalZtreeList.add(ztree);
        OutZtreeService service = new OutZtreeServiceImpl();
        ztree = new Ztree();
        ztree.setId("4");
        ztree.setpId("5");
        service.getAncestorNodeList(ztree, totalZtreeList, resultZtreeList);
        System.out.println(resultZtreeList.size());
        for(Ztree ztree1 : resultZtreeList){
            System.out.println(ztree1.getId()+"-"+ztree1.getpId());
        }
    }
}
